/*******************************************************************************
 * Copyright (c) 2011 LegSem.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/
package com.legstar.test.coxb.lsfileae.bind;

import com.legstar.coxb.ICobolBinding;
import com.legstar.coxb.common.CComplexBinding;
import com.legstar.coxb.ICobolStringBinding;
import com.legstar.coxb.CobolBindingFactory;
import com.legstar.coxb.ICobolBindingFactory;
import com.legstar.coxb.ICobolComplexBinding;
import com.legstar.coxb.host.HostException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.legstar.test.coxb.lsfileae.ComPersonal;
import com.legstar.test.coxb.lsfileae.ObjectFactory;

/**
 * LegStar Binding for Complex element :
 *   ComPersonal.
 * 
 * This class was generated by LegStar Binding generator.
 */
public class ComPersonalBinding 
             extends CComplexBinding {

    /** Value object to which this cobol complex element is bound. */
    private ComPersonal mValueObject;
  
    /** Indicates that the associated Value object just came from the constructor
     * and doesn't need to be recreated. */
    private boolean mUnusedValueObject = false;
    
    /** Maximum host bytes size for this complex object. */
    private static final int BYTE_LENGTH = 48;
    
    /** Child bound to value object property ComName(String). */
    public ICobolStringBinding _comName;
    /** Child bound to value object property ComAddress(String). */
    public ICobolStringBinding _comAddress;
    /** Child bound to value object property ComPhone(String). */
    public ICobolStringBinding _comPhone;
            
    /** Logger. */
    private final Log _log = LogFactory.getLog(getClass());

    /** Binding factory. */
    private static final ICobolBindingFactory BF
        = CobolBindingFactory.getBindingFactory();
 
    /** Static reference to Value object factory to be used as default. */
    private static final ObjectFactory JF = new ObjectFactory();
    
    /** Current Value object factory (Defaults to the static one but can be
     *  changed). */
    private ObjectFactory mValueObjectFactory = JF;
    
    /**
     * Constructor for a root Complex element without a bound Value object.
     */
    public ComPersonalBinding() {
        this(null);
    }

    /**
     * Constructor for a root Complex element with a bound Value object.
     * 
     * @param valueObject the concrete Value object instance bound to this
     *        complex element
     */
    public ComPersonalBinding(
            final ComPersonal valueObject) {
        this("", "", null, valueObject);
    }

    /**
    * Constructor for a Complex element as a child of another element and
    * an associated Value object.
    * 
    * @param bindingName the identifier for this binding
    * @param fieldName field name in parent Value object
    * @param valueObject the concrete Value object instance bound to this
    *        complex element
    * @param parentBinding a reference to the parent binding
    */
    public ComPersonalBinding(
            final String bindingName,
            final String fieldName,
            final ICobolComplexBinding parentBinding,
            final ComPersonal valueObject) {
        
        super(bindingName, fieldName, ComPersonal.class, null, parentBinding);
        mValueObject = valueObject;
        if (mValueObject != null) {
            mUnusedValueObject = true;
        }
        initChildren();
        setByteLength(BYTE_LENGTH);
    }

    /** Creates a binding property for each child. */
    private void initChildren() {
        if (_log.isDebugEnabled()) {
            _log.debug("Initializing started");
        }
        /* Create binding children instances */

        _comName = BF.createStringBinding("ComName",
               "ComName", String.class, this);
        _comName.setCobolName("COM-NAME");
        _comName.setByteLength(20);
        _comAddress = BF.createStringBinding("ComAddress",
               "ComAddress", String.class, this);
        _comAddress.setCobolName("COM-ADDRESS");
        _comAddress.setByteLength(20);
        _comPhone = BF.createStringBinding("ComPhone",
               "ComPhone", String.class, this);
        _comPhone.setCobolName("COM-PHONE");
        _comPhone.setByteLength(8);

        /* Add children to children list */
        getChildrenList().add(_comName);
        getChildrenList().add(_comAddress);
        getChildrenList().add(_comPhone);
 
        if (_log.isDebugEnabled()) {
            _log.debug("Initializing successful");
        }
    }
    
    /** {@inheritDoc} */
    public void createValueObject() throws HostException {
        /* Since this complex binding has a constructor that takes a
         * Value object, we might already have a Value object that
         * was not used yet. */
        if (mUnusedValueObject && mValueObject != null) {
            mUnusedValueObject = false;
            return;
        }
        mValueObject = mValueObjectFactory.createComPersonal();
    }

    /** {@inheritDoc} */
    public void setChildrenValues() throws HostException {

         /* Make sure there is an associated Value object*/
        if (mValueObject == null) {
            createValueObject();
        }
        /* Get Value object property _comName */
        if (_log.isDebugEnabled()) {
            _log.debug("Getting value from Value object property "
                    + "_comName"
                    + " value=" + mValueObject.getComName());
        }
        _comName.setObjectValue(mValueObject.getComName());
        /* Get Value object property _comAddress */
        if (_log.isDebugEnabled()) {
            _log.debug("Getting value from Value object property "
                    + "_comAddress"
                    + " value=" + mValueObject.getComAddress());
        }
        _comAddress.setObjectValue(mValueObject.getComAddress());
        /* Get Value object property _comPhone */
        if (_log.isDebugEnabled()) {
            _log.debug("Getting value from Value object property "
                    + "_comPhone"
                    + " value=" + mValueObject.getComPhone());
        }
        _comPhone.setObjectValue(mValueObject.getComPhone());
     }

    /** {@inheritDoc} */
    public void setPropertyValue(final int index) throws HostException {
 
        ICobolBinding child = getChildrenList().get(index);
        
       /* Children that are not bound to a value object are ignored.
        * This includes Choices and dynamically generated counters
        * for instance.  */
        if (!child.isBound()) {
            return;
        }
        
        /* Set the Value object property value from binding object */
        Object bindingValue = null;
        switch (index) {
        case 0:
            bindingValue = child.getObjectValue(String.class);
            mValueObject.setComName((String) bindingValue);
            break;
        case 1:
            bindingValue = child.getObjectValue(String.class);
            mValueObject.setComAddress((String) bindingValue);
            break;
        case 2:
            bindingValue = child.getObjectValue(String.class);
            mValueObject.setComPhone((String) bindingValue);
            break;
         default:
            break;
        }
        if (_log.isDebugEnabled()) {
            _log.debug("Setting value of Value object property "
                    + child.getJaxbName()
                    + " value=" + bindingValue);
        }
    }

    /** {@inheritDoc} */
    public Object getObjectValue(
            final Class < ? > type) throws HostException {
        if (type.equals(ComPersonal.class)) {
            return mValueObject;
        } else {
            throw new HostException("Attempt to get binding " + getBindingName()
                    + " as an incompatible type " + type);
        }
    }

    /** {@inheritDoc} */
    public void setObjectValue(
            final Object bindingValue) throws HostException {
        if (bindingValue == null) {
            mValueObject = null;
            return;
        }
        if (bindingValue.getClass().equals(ComPersonal.class)) {
            mValueObject = (ComPersonal) bindingValue;
        } else {
            throw new HostException("Attempt to set binding " + getBindingName()
                    + " from an incompatible value " + bindingValue);
        }
    }

    /**
     * @return the java object factory for objects creation
     */
    public ObjectFactory getObjectFactory() {
        return mValueObjectFactory;
    }

    /**
     * @param valueObjectFactory the java object factory for objects creation 
     */
    public void setObjectFactory(final Object valueObjectFactory) {
        mValueObjectFactory = (ObjectFactory) valueObjectFactory;
    }

    /** {@inheritDoc} */
    public boolean isSet() {
        return (mValueObject != null);
    }

    /**
     * @return the bound Value object
     */
    public ComPersonal getComPersonal() {
        return mValueObject;
    }
    
    /**
     * The COBOL complex element maximum length in bytes.
     * 
     * @return COBOL complex element maximum length in bytes
     */
    public int getByteLength() {
        return BYTE_LENGTH;
    }
}

